class MyApp extends App {
    constructor() {
        super();
    }

    onCreate() {
        this.count = 0;
        this.canvas = new Canvas(1)
        console.log('canvas:width:', this.canvas.width, 'canvas:height:', this.canvas.height);
        this.context = this.canvas.getContext();
        if (this.context) {
            this.context.fillText('Hello, from Skia Binding JS', 0, 100);
            this.context.fillText('落霞与孤鹜齐飞, 秋水共长天一色', 0, 200);
        }
        this.canvas.commit();
        console.log('onCreate from js\n');
        this.shouldDraw = true;

    }

    onResume() {
        this.shouldDraw = true;
        console.log("onResume from js two \n");
    }

    onPause() {
        this.shouldDraw = false;
        console.log("onPause from js");
    }

    onStop() {
        this.shouldDraw = false;
    }

    onDestroy() {
        this.shouldDraw = false;
        os.clearTimeout(this.handle);
        console.log("clearTimeout");
    }

    draw() {
        console.log('draw');
        this.count++;
        this.context.clearRect(0, 500, 800, 700);
        this.context.fillText('当前已经运行了: ' + this.count + 's', 0, 600);
        this.canvas.commit();
        console.log('draw finished');
    }

    shouldDrawIt() {
        return this.shouldDraw
    }
};

var app = new MyApp();
registerApp(app);
function doDrawFunction() {
    if (app.shouldDrawIt()) {
        app.draw();
    }
    return app.handle = os.setTimeout(doDrawFunction, 1000);
}
os.setTimeout(function () {
    doDrawFunction();
}, 1)
